// WARNING: This file has been generated. Any manual changes must be made within preserved regions or they will be lost.

//===============================================================================
//
// HabitatTests
//
// PURPOSE: 
// Tests the functionality of Habitat.
//
// NOTES: 
// 
//
//===============================================================================
//
// Copyright (C) 2003 Wallis Software Solutions
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//
//===============================================================================

namespace Agile.Common.Tests
{
    using System;
    using System.Globalization;
    using System.Threading;
    using Agile.Common;
    using Agile.Testing;
    using NUnit.Framework;
        /// <summary>
        /// Tests the functionality of Habitat.
        /// </summary>
    [TestFixture]
    public class HabitatTests : AgileTestBase
    {

        #region Preserved Region - Developer Hand Written Code

        #region Test now

        /// <summary>
        /// Event handler for On
        /// </summary>
        /// <param name="systemTime">the system time</param>
        /// <param name="effectiveTime">the effective time</param>
        private void TimeChanged(DateTime systemTime, DateTime effectiveTime)
        {
            _systemTimePassed = systemTime;
            _effectiveTimePassed = effectiveTime;
            _timeChangedFired = true;
        }

        private bool _timeChangedFired;
        private DateTime _systemTimePassed;
        private DateTime _effectiveTimePassed;


        /// <summary>
        /// This makes sure Now works as expected
        ///		- check that Now is the same as datetime now
        ///		- fake now
        ///     - ensure event fired (and reset)
        ///     - check event parameters
        ///		- wait three seconds
        ///		- check that now is different
        ///		- reset now
        ///     - ensure event fired again (and reset)
        ///     - check event parameters
        ///		- check that now is the same as datetime now
        /// </summary>
        [Test]
        public void TestNow()
        {
            //		- check that Now is the same as datetime now
            Assert.IsTrue(Dates.SameDate(DateTime.Now, Habitat.Now)
                , "check that Now is the same as datetime now");

            //      - reset and attach handler
            _timeChangedFired = false;
            Habitat.OnSetApparentTime = new SetApparentTimeHandler(TimeChanged);

            //		- fake now
            DateTime fakeTime = DateTime.Parse("1 Jan 2003 11:03 am", CultureInfo.CurrentCulture);
            Habitat.SetApparentTimeTo(fakeTime);
            try
            {
                //      - ensure event fired (and reset)
                Assert.IsTrue(_timeChangedFired, "Check time changed event fired");
                _timeChangedFired = false;

                //      - check event parameters
                Assert.IsTrue(_effectiveTimePassed.Equals(fakeTime), "Check system time parameter is correct");
                Assert.IsTrue(Dates.SameDate(_systemTimePassed, DateTime.Now), "Check effective time parameter is correct");

                //		- wait three seconds
                Thread.Sleep(3000);

                //		- check that now is different
                Assert.IsTrue(Dates.SameDate(Habitat.Now, fakeTime.AddSeconds(3))
                    , "Check that now is what it should be");

                //		- reset now
                Habitat.GoBackToUsingSystemTime();

                //      - ensure event fired again (and reset)
                Assert.IsTrue(_timeChangedFired, "Check time changed event fired again");
                _timeChangedFired = false;

                //      - check event parameters
                Assert.IsTrue(_effectiveTimePassed.Equals(_systemTimePassed), "Check system time parameter equals effective time parameter");

                //		- check that now is the same as datetime now
                Assert.IsTrue(Dates.SameDate(DateTime.Now, Habitat.Now), "check that Now is the same as datetime now");

            }
            finally
            {
                Habitat.GoBackToUsingSystemTime();
            }
        }

        #endregion

        #region Test Fake Machine name

        /// <summary>
        /// This tests faking the machine name
        ///		- check the machine name is the real machine name
        ///		- fake the machine name to whatever
        ///		- check the machine name is whatever
        ///		- restore the machine name
        ///		- check the machine name is the real machine name
        /// </summary>
        [Test]
        public void TestFakeMachineName()
        {
            //		- check the machine name is the real machine name
            Assert.AreEqual(Environment.MachineName, Habitat.MachineName
                , "check the machine name is the real machine name");

            //		- fake the machine name to whatever
            Habitat.FakeMachineName("Whatever");

            //		- check the machine name is whatever
            Assert.AreEqual("Whatever", Habitat.MachineName
                , "check the machine name is whatever");

            //		- restore the machine name
            Habitat.RestoreMachineName();

            //		- check the machine name is the real machine name
            Assert.AreEqual(Environment.MachineName, Habitat.MachineName
                , "check the machine name is the real machine name");
        }

        #endregion

        #region Test FakeTime

        /// <summary>
        /// Test faking the time
        ///     - record the year
        ///     - set the fake time to 1/1/1970
        ///     - check the year is 1970
        ///     - finish, check the year is the original
        ///     - set the fake time to 1/1/1970
        ///     - throw an exception
        ///     - catch the exception
        ///     - check that the year is the original
        /// </summary>
        [Test]
        public void TestFakeTime()
        {
            //     - record the year
            int year = Habitat.Now.Year;

            //    - set the fake time to 1/1/1970
            using (Habitat.FakeTime("1/1/1970"))
            {
                //     - check the year is 1970
                Assert.AreEqual(1970, Habitat.Now.Year
                    , "check the year is 1970");
            }
            //     - finish, check the year is the original
            Assert.AreEqual(year, Habitat.Now.Year
                , "finish, check the year is the original");

            //     - set the fake time to 1/1/1970
            try
            {
                using (Habitat.FakeTime(Convert.ToDateTime("1/1/1970", CultureInfo.CurrentCulture)))
                {
                    //     - throw an exception
                    throw new AgileCommonException("This should be thrown");

                }
            }
                //     - catch the exception
            catch (AgileCommonException)
            {}

            //     - check that the year is the original
            Assert.AreEqual(year, Habitat.Now.Year
                , "check that the year is the original");
        }

        #endregion

        #region Test faketime inside fake time

        /// <summary>
        /// This checks faking a time inside another faked time
        ///     - save the year
        ///     - fake time to 1/jun/1981
        ///     - check year is 1981
        ///     - fake time to 26/sep/1971
        ///     - check year is 1971
        ///     - finish... check year is 1981
        ///     - finish... check year is original
        /// </summary>
        [Test]
        public void TestFakeTimeInsideFakeTime()
        {
            //     - save the year
            int year = Habitat.Now.Year;

            //     - fake time to 1/jun/1981
            using (Habitat.FakeTime("1/Jun/1981"))
            {
                //     - check year is 1981
                Assert.AreEqual(1981, Habitat.Now.Year, "check year is 1981");

                //     - fake time to 26/sep/1971
                using (Habitat.FakeTime("26/sep/1971"))
                {
                    //     - check year is 1971
                    Assert.AreEqual(1971, Habitat.Now.Year, "check year is 1971");
                }
                //     - finish... check year is 1981
                Assert.AreEqual(1981, Habitat.Now.Year, "check year is 1981 afterwards");
            }
            //     - finish... check year is original
            Assert.AreEqual(year, Habitat.Now.Year
                , "finish... check year is original");
        }

        #endregion

        //            #region Test ForSerialization
        //        
        //            /// <summary>
        //            /// Test for serialization function
        //            ///     - get the package for serialization
        //            ///     - serialize it to a string
        //            ///     - make sure we can read it into an xml document
        //            /// </summary>
        //            [Test]
        //            public void TestForSerialization()
        //            {
        //                //     - get the package for serialization
        //                SerializingPackage package = Habitat.ForSerialization;
        //
        //                //     - serialize it to a string
        //                string theXML = CommonFunctions.XMLSerialize( package );
        //    
        //                //     - make sure we can read it into an xml document
        //                XmlDocument newDocument = new XmlDocument();
        //                newDocument.LoadXml( theXML );
        //            }
        //
        //
        //            #endregion

        #region User and Domain tests - need to be fixed when need to include active directory stuff

        //            #region Check Local domain name
        //            /// <summary>
        //            /// Checks we can get the local domain name
        //            ///     - get the local domain name
        //            ///     - check buildmachine is a valid login in that domain
        //            /// </summary>
        //            [Test]
        //            public void CheckLocalDomainName()
        //            {
        //                //     - get the local domain name
        //                string domain = Habitat.LocalDomainName;
        //                Assert.IsNotNull( domain ,  "check domain is not null");
        //                Assert.IsTrue( domain.Length > 0 , "Check domain length is not 0");
        //
        //                //     - check buildmachine is a valid login in that domain
        //                Assert.AreEqual("check buildmachine is a valid login in that domain",
        //                    CommonFunctions.Login( domain, "buildmachine", "BuildMachine"));
        //            }
        //
        //            #endregion
        //            #region Test User
        //
        //            /// <summary>
        //            /// Test the user functions
        //            ///		- check that the current user is not blank
        //            ///		- simulate running as a different user
        //            ///		- check that if we ask for the user, we get our user
        //            ///		- reset the user
        //            ///		- check that if we ask for the user, we get original user
        //            /// </summary>
        //            [Test]
        //            public void TestUser()
        //            {
        //                //		- check that the current user is not blank
        //                string user = Habitat.CurrentUser;
        //                Assert.AreEqual( "check that the current user is not blank",
        //                    user.Length != 0 );
        //
        //                //		- simulate running as a different user
        //                Habitat.PretendWeAreRunningAsUser("sillyUser", "", "");
        //
        //                //		- check that if we ask for the user, we get our user
        //                Assert.AreEqual( "check that if we ask for the user, we get our user",
        //                    Habitat.CurrentUser == "sillyUser" );
        //			
        //                //		- reset the user
        //                Habitat.GoBackToRunningAsSystemUser();
        //
        //                //		- check that if we ask for the user, we get original user
        //                Assert.AreEqual( "check we get original user", Habitat.CurrentUser == user );
        //            }
        //

        //            #endregion
        //            #region Test Pretending to be AD User
        //            /// <summary>
        //            /// Verifies that we can fake using an existing AD user by setting the Habitat
        //            ///		- Store the existing habitat user
        //            ///		- Check current user from the Active Directory is same as existing Habitat user
        //            ///		- Fake running as the buildmachine user
        //            ///		- get the current user from the Active Directory
        //            ///		- Check current user is BuildMachine
        //            ///		- Reset the user in the habitat.
        //            ///		- Check current user from the Active Directory is the same as the original user.
        //            /// </summary>
        //            [Test]
        //            public void TestPretendingToBeADUser()
        //            {
        //                // Store the existing habitat user
        //                string user = Habitat.CurrentUser;
        //                // Check current user from the Active Directory is same as existing Habitat user
        //                ActiveDirectoryUser adUser = ActiveDirectory.CurrentUser;
        //                string adLoginName = adUser.LoginName;
        //                Assert.AreEqual( adLoginName.ToLower(), user.ToLower(), "Check current user from the Active Directory is same as existing Habitat user");
        //
        //                // Fake running as the buildmachine user
        //                Habitat.PretendWeAreRunningAsUser("BuildMachine", "", "9279a13b2f013a4294b204dc79b0df61");
        //                // get the current user from the Active Directory
        //                adUser = ActiveDirectory.CurrentUser;
        //                adLoginName = adUser.LoginName;
        //                // Check current user is BuildMachine
        //                Assert.AreEqual("buildmachine", adLoginName.ToLower(), "Check current user is BuildMachine");
        //
        //                // Reset the user in the habitat.
        //                Habitat.GoBackToRunningAsSystemUser();
        //                adUser = ActiveDirectory.CurrentUser;
        //                // Check current user from the Active Directory is the same as the original user.
        //                adLoginName = adUser.LoginName;
        //                Assert.AreEqual( user.ToLower(), adLoginName.ToLower(), "Check current user from the Active Directory is the same as the original user.");
        //            }
        //            #endregion

        #endregion

        #endregion // Preserved Region - Developer Hand Written Code

    }

}
